﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HLSL
{
    public class ProjectedTextureMaterial:Material
    {
        public Vector3 ProjectorPosition { get; set; }
        public Vector3 ProjectorTarget { get; set; }
        public Texture2D ProjectedTexture { get; set; }
        public bool ProjectorEnable { get; set; }
        public float Scale { get; set; }

        float haftWidth, haftHeight;
        public ProjectedTextureMaterial(Texture2D Texture, GraphicsDevice GraphicsDevice)
        {
            this.ProjectorPosition = new Vector3(1500, 1500, 1500);
            this.ProjectorTarget = new Vector3(0, 150, 0);
            this.ProjectedTexture = Texture;
            this.ProjectorEnable = true;
            this.Scale = 1;
            this.haftHeight = (float)Texture.Height / 2;
            this.haftWidth = (float)Texture.Width / 2;

        }
        public override void SetEffectParamaters(Effect effect)
        {
            if (effect.Parameters["ProjectorEnable"] != null)
                effect.Parameters["ProjectorEnable"].SetValue(ProjectorEnable);
            if (!ProjectorEnable)
            {
                return;
            }
            Matrix projection = Matrix.CreateOrthographicOffCenter(-haftWidth * Scale, haftWidth * Scale, -haftHeight * Scale, haftHeight * Scale, -100000, 100000);
            Matrix view = Matrix.CreateLookAt(ProjectorPosition, ProjectorTarget, Vector3.Up);
            if (effect.Parameters["ProjectorViewProjection"] != null)
            {
                effect.Parameters["ProjectorViewProjection"].SetValue(view * projection);
            }
            if (effect.Parameters["ProjectedTexture"] != null)
            {
                effect.Parameters["ProjectedTexture"].SetValue(ProjectedTexture);
            }
        }
    }
}
